package com.ksd.common.weather;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ksd.common.bean.WeatherResponse;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;

public class WeatherByAMapAPI {
    // 替换为你在高德开放平台申请的 API Key
    private static final String API_KEY = "5a60d68090e740d7bb515ea75a85cda1";
    // 高德地图天气查询 API 地址
    private static final String API_URL = "https://restapi.amap.com/v3/weather/weatherInfo";

    // 发送 HTTP 请求获取天气数据
    public static CompletableFuture<String> fetchWeatherData(String city, String extensions) {
        HttpClient client = HttpClient.newHttpClient();
        String query = String.format("key=%s&city=%s&extensions=%s", API_KEY, city, extensions);
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(API_URL + "?" + query))
                .build();

        return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body);
    }

    // 解析天气数据并打印相关信息
    public static WeatherResponse parseAndPrintWeather(String weatherData) {
        Gson gson = new Gson();
//        System.out.println("weatherData:"+weatherData);
        JsonObject jsonObject = JsonParser.parseString(weatherData).getAsJsonObject();
        WeatherResponse weatherResponse = new WeatherResponse();
        if (jsonObject.get("status").getAsString().equals("1")) {
            JsonObject forecasts = jsonObject.getAsJsonArray("forecasts").get(0).getAsJsonObject();
//            System.out.println("城市：" + forecasts.get("city").getAsString());
//            System.out.println("天气预报时间：" + forecasts.get("reporttime").getAsString());
            JsonObject casts = forecasts.getAsJsonArray("casts").get(0).getAsJsonObject();
//            System.out.println("天气状况：" + casts.get("dayweather").getAsString());
//            System.out.println("最高温度：" + casts.get("daytemp").getAsString() + "℃");
//            System.out.println("最低温度：" + casts.get("nighttemp").getAsString() + "℃");
//            System.out.println("白天天气状态：" + casts.get("dayweather").getAsString());
//            System.out.println("晚上天气状态：" + casts.get("nightweather").getAsString());

            weatherResponse.setDaytemp(casts.get("daytemp").getAsString());
            weatherResponse.setNighttemp(casts.get("nighttemp").getAsString());
            weatherResponse.setDayweather(casts.get("dayweather").getAsString());
            weatherResponse.setNightweather(casts.get("nightweather").getAsString());
            weatherResponse.setReporttime(forecasts.get("reporttime").getAsString());
            weatherResponse.setStatus("1");
        } else {
            System.out.println("获取天气数据失败，错误信息：" + jsonObject.get("info").getAsString());
            weatherResponse.setStatus("-1");
            weatherResponse.setErrorMessage("获取天气数据失败，错误信息：" + jsonObject.get("info").getAsString());
        }
        return weatherResponse;
    }

    public static void main(String[] args) {
        String city = "北京市"; // 替换为你要查询天气的城市名称或城市编码
        String extensions = "all"; // 扩展参数，all 表示获取全部天气信息
        fetchWeatherData(city, extensions)
                .thenApply(WeatherByAMapAPI::parseAndPrintWeather)
                .thenAccept(weatherResponse -> {
                    if (weatherResponse.getStatus().equals("1")) {
                        System.out.println("最新天气状态获取成功");
                        System.out.println("最高温度: " + weatherResponse.getDaytemp() + "℃");
                        System.out.println("最低温度: " + weatherResponse.getNighttemp() + "℃");
                        System.out.println("白天天气状态: " + weatherResponse.getDayweather());
                        System.out.println("晚上天气状态: " + weatherResponse.getNightweather());
                        System.out.println("天气预报时间: " + weatherResponse.getReporttime());
                    } else {
                        System.out.println("获取天气状态失败: " + weatherResponse.getErrorMessage());
                    }
                })
                .exceptionally(ex -> {
                    System.out.println("发生异常: " + ex.getMessage());
                    return null;
                });

        // 防止主线程提前退出
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}